class PagesController < ApplicationController
  before_filter :current_user

  layout 'application'

  def index
    @hotels = Hotel.all

    respond_to do |format|
      format.html # index.html.erb
     # format.json { render json: @index }
    end   
  end

  def hotel_index
    @hotels = Hotel.joining_and_search(hotel_search,property_type,price_from,price_until,star_from,star_until,area)
                    .order( sort_column + ' DESC ' )
                    .paginate(:per_page => 5, :page => params[:page])
    
    respond_to do |format|
      format.html # index.html.erb
      format.js { render layout: false }
    end  
  end

  def hotel_detail
    if params[:hid]
      @hotel = Hotel.find(params[:hid])
      @hotel_images = HotelImage.where('hotel_id =?', params[:hid])

      respond_to do |format|
        format.html # index.html.erb
        format.js { render layout: false }
      end 
    end
  end
   
  def tour_index
    @tours = Tour.joining_and_search(day_start,day_end,area,price_from,price_until,tour_type)
                    .order( sort_column + ' DESC ' )
                    .paginate(:per_page => 2, :page => params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.js { render layout: false }
    end   
  end

  def tour_detail
    if params[:tid]
      @tour = Tour.find(params[:tid])
      @tour_images = TourImage.where('tour_id =?', params[:tid])

      respond_to do |format|
        format.html # index.html.erb
        format.js { render layout: false }
      end 
    end
  end

  def transport_index
    @transports = Transport.all

    respond_to do |format|
      format.html # index.html.erb
      format.js { render layout: false }
    end 
  end

  def transport_detail
    @transport_packages = TransportPackage.where('transport_id =?', params[:tsid])

    respond_to do |format|
      format.html # index.html.erb
      format.js { render layout: false }
    end 
  end

  def test

    respond_to do |format|
      format.html 
      format.js { render layout: false }
    end   
  end
    

  private

    def sort_column
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'stars' ? params[:sort] : "created_at"
      Hotel.column_names.include?(params[:sort]) || params[:sort] == 'is_on_promoted' ? params[:sort] : "created_at"
    end

    def tour_type
      if params[:tour_type] 
        session[:tour_type] = params[:tour_type]
      else params[:clear_session]
        session[:tour_type] = nil
      end
      results = session[:tour_type]
      results
    end

    def day_start
      if params[:day_start] 
        session[:day_start] = params[:day_start]
      else params[:clear_session]
        session[:day_start] = nil
      end
      results = session[:day_start]
      results
    end

    def day_end
       if params[:day_end] 
        session[:day_end] = params[:day_end]
      else params[:clear_session]
        session[:day_end] = nil
      end
      results = session[:day_end]
      results
    end 

    def hotel_search
      if params[:search] 
        session[:hotel_search] = params[:search]
      else params[:clear_session]
        session[:hotel_search] = nil
      end
      results = session[:hotel_search]
      results
    end

    def property_type
      if params[:property_type]
        session[:property_type] = params[:property_type]
      else params[:clear_session]
        session[:property_type] = nil
      end
      results = session[:property_type]
      results
    end 

    def area
      if params[:area]
        session[:area] = params[:area]
      else params[:clear_session]
        session[:area] = nil
      end
      results = session[:area]
      results
    end

    def facility
      if params[:facility]
        session[:facility] = params[:facility]
      else params[:clear_session]
        session[:facility] = nil
      end
      results = session[:facility]
      results
    end

    def price_from
      if params[:price_from] 
        session[:price_hotel_from] = params[:price_from]
      else params[:clear_session]
        session[:price_hotel_from] = nil
      end
      result1 = session[:price_hotel_from]
    end

    def price_until
      if params[:price_until]
        session[:price_hotel_until] = params[:price_until]
      else params[:clear_session]
        session[:price_hotel_until] = nil
      end
      result2 = session[:price_hotel_until]
    end

    def star_from
      if params[:star_from]
        session[:star_from] = params[:star_from]
      else params[:clear_session]
        session[:star_from] = nil
      end
      result = session[:star_from]
    end

    def star_until
      if params[:star_until]
        session[:star_until] = params[:star_until]
      else params[:clear_session]
        session[:star_until] = nil
      end
      result = session[:star_until]
    end

end
